﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    public class UCLLCLBusiness
    {
        private decimal ucl;

        private decimal lcl;

        private decimal uclr;



        public UCLLCLBusiness(decimal xbar, decimal rBar, int isFixed, int isUCLRFixed, string testPlanID, string functionalTestID)
        {
            if (isFixed == 0 && isUCLRFixed == 0)
            {
                ucl = Math.Round(xbar + 1.880M * rBar, 2);
                lcl = Math.Round(xbar - 1.880M * rBar, 2);
                uclr = Math.Round(rBar * 3.267M, 2);
            }
            else
            {
                using (Production2Entities entity = new Production2Entities())
                {
                    var result = (from ft in entity.V_CAL_Part_Functional_Test
                                  where ft.test_series_id == testPlanID
                                  && ft.functional_test_id == functionalTestID
                                  && ft.latest_record == "Y"
                                  select new { ft.UCL, ft.LCL, ft.UCL_R}).FirstOrDefault();
                    ucl = result.UCL;
                    lcl = result.LCL;
                    uclr = result.UCL_R;
                }

                if (isUCLRFixed == 0)
                {
                    uclr = Math.Round(rBar * 3.267M, 2);
                }

                if (isFixed == 0)
                {
                    ucl = Math.Round(xbar + 1.880M * rBar, 2);
                    lcl = Math.Round(xbar - 1.880M * rBar, 2);
                }
            }

          

        }

        public decimal UCL
        {
            get { return ucl; }
        }

        public decimal LCL
        {
            get { return lcl; }
        }

        public decimal UCLR
        {
            get{return uclr;}
        }

    }
}
